﻿import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:vischat/core/annotations/getx_route_enhanced.dart';
import 'package:vischat/modules/home/socialize/chat/chat_binding.dart';
import 'package:vischat/modules/home/socialize/chat/chat_controller.dart';
import 'package:vischat/modules/home/socialize/chat/widgets/message_input/message_input.dart';
import 'package:vischat/modules/home/socialize/chat/widgets/message_input/message_input_controller.dart';
import 'package:vischat/modules/home/socialize/chat/widgets/message_item/message_item.dart';
import 'package:vischat/shared/widgets/tap_to_close_keyboard.dart';

@GetXRoute('/chat', binding: ChatBinding)
class ChatPage extends GetView<ChatController> {
  const ChatPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('${controller.conversation.showName}')),
      body: SafeArea(
        child: Column(
          children: [
            Expanded(
              child: TapToCloseKeyboard(
                callback: () {
                  Get.find<MessageInputController>().hideOptionsPanel();
                  Get.find<MessageInputController>().hideEmoji();
                },
                child: EasyRefresh(
                  controller: controller.refreshController,
                  onLoad: null,
                  onRefresh: controller.loadMessages,
                  header: ClassicHeader(showText: false),
                  footer: null,
                  child: GetX<ChatController>(
                    builder:
                        (_) => CustomScrollView(
                          controller: controller.scrollController,
                          reverse: true,
                          physics: const ClampingScrollPhysics(),
                          slivers: [
                            if (controller.isLoading)
                              SliverToBoxAdapter(
                                child: Center(
                                  child: Padding(
                                    padding: EdgeInsets.all(20.w),
                                    child: CircularProgressIndicator(),
                                  ),
                                ),
                              ),
                            SliverList(
                              delegate: SliverChildBuilderDelegate(
                                (context, index) => MessageItem(index),
                                childCount: controller.messages.length,
                                addAutomaticKeepAlives: false,
                                addRepaintBoundaries: true,
                              ),
                            ),
                            if (controller.newMessageCount > 0)
                              SliverToBoxAdapter(
                                child: GestureDetector(
                                  onTap: controller.jumpToLatest,
                                  child: Container(
                                    margin: EdgeInsets.all(10.w),
                                    padding: EdgeInsets.symmetric(
                                      horizontal: 20.w,
                                      vertical: 10.h,
                                    ),
                                    decoration: BoxDecoration(
                                      color: Colors.blue,
                                      borderRadius: BorderRadius.circular(20),
                                    ),
                                    child: Text(
                                      'page.chat.unread'.trParams({
                                        "count":
                                            "${controller.newMessageCount}",
                                      }),
                                      style: TextStyle(color: Colors.white),
                                    ),
                                  ),
                                ),
                              ),
                          ],
                        ),
                  ),
                ),
              ),
            ),
            MessageInputBar(),
          ],
        ),
      ),
    );
  }
}
